package org.jpass.dc.business.user.dao;

import java.util.ArrayList;
import java.util.List;

import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.bean.Page;
import org.jpass.dc.common.dao.GenericDao;
import org.jpass.dc.common.util.RefParaValue;

@SuppressWarnings("rawtypes")
public class UserManageDaoImpl extends GenericDao implements UserManageDao {

	@SuppressWarnings("unchecked")
	public UserManageDaoImpl() {
		super(UserVOBean.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addUser(UserVOBean voBean) throws Exception {
		this.save(voBean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateUser(UserVOBean user) throws Exception {
		this.update(user);
	}

	public UserVOBean findUserByEmail(String email) throws Exception {
		String hql = "";
		if (email.contains("@")) {// 判断用户是否用邮箱登陆
			hql = "from UserVOBean user where user.email= '" + email + "'";
		} else {
			hql = "from UserVOBean user where user.username= '" + email + "'";
		}
		UserVOBean voBean = (UserVOBean) this.query(hql);
		return voBean;
	}

	@Override
	public List findAllUserList() throws Exception {
		List list = new ArrayList();
		String hql = "from UserVOBean user";
		list = this.queryAllUserList(hql);
		return list;
	}

	@Override
	public List findBackUserList(Page page, String findUsername)
			throws Exception {
		List list = new ArrayList();
		String countHql = "select count(*) from UserVOBean user ";
		StringBuffer searchHql = new StringBuffer(
				"select new Map(A.id as id, A.username as username, A.password as password, A.email as email, A.sex as sex,"
						+ "A.birthday as birthday, A.point as point,A.group.groupName as groupName, A.workPath as workPath ,"
						+ " A.createdDate as createdDate, A.isActivated as isActivated, A.isMember as isMember,"
						+ " A.memBeginTime as memBeginTime, A.memEndTime as memEndTime, A.isOnline as isOnline, A.loginTimes as loginTimes,"
						+ " A.lastSignTime as lastSignTime, A.notes as notes)"
						+ " from UserVOBean as A ");
		if(!RefParaValue.empty(findUsername)){
			String str = " where A.username like '%"+ findUsername +"%'";
			searchHql.append(str);
		}
		searchHql.append(" order by A.createdDate desc ");
		list = this.queryForList(countHql, searchHql.toString(), null, page);
		return list;
	}
	
	@Override
	public UserVOBean findUserById(String userId) throws Exception {
		UserVOBean user = null;
		String hql = "";
		if (!RefParaValue.empty(userId)) {
			hql = "from UserVOBean user where user.id= '" + userId + "'";
			user = (UserVOBean) this.query(hql);
		}
		return user;
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean deleteUserObj(UserVOBean user) throws Exception {
		this.delete(user);
		return true;
	}

}
